package com.abocode.aichat.core.application;
import com.abocode.aichat.core.interfaces.constant.ApplicationCostant;
import com.abocode.aichat.server.channel.ChannelWrapper;
import com.abocode.aichat.server.context.ServerRuntimeContext;
import com.abocode.aichat.server.entity.Entity;
import com.abocode.aichat.server.entity.EntityImpl;
import com.abocode.aichat.server.modules.ModuleHandler;
import com.abocode.aichat.server.pipeline.MessagePipeline;
import com.abocode.aichat.server.reply.Replies;
import com.abocode.aichat.server.stanza.Stanza;
import com.abocode.aichat.server.stanza.StanzaWrapper;
import io.netty.channel.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by franky.
 */
@Service
public class ChatMessageHandler implements ModuleHandler {
    @Autowired
    private MessagePipeline messagePipeline;
    @Autowired
    private UserService userService;
    public Replies execute(ChannelWrapper channelWrapper, Stanza stanza, ServerRuntimeContext serverRuntimeContext) {
        Channel fromChannel = channelWrapper.getChannel();
        Map body = stanza.body();
        String toJid = String.valueOf(body.get("to"));
        String fromJid = String.valueOf(body.get("from"));
        Replies replies = new Replies();
        //判断用户是否是黑名单
        Entity toEntity=null;
        try {
             toEntity = EntityImpl.parse(toJid,true);
        }catch (Exception e){
            replies.addReply(fromChannel, StanzaWrapper.wrapper(stanza, 201));
            return replies;
        }
        //解密fromJid
        Entity fromEntity = EntityImpl.parse(fromJid);
        userService.isFromLocked(fromEntity);
        if(userService.isToLocked(toEntity)){
            replies.addReply(fromChannel, StanzaWrapper.wrapper(stanza, 203));
            return replies;
        }

        if(userService.isBlankList(fromEntity,toEntity)){
            replies.addReply(fromChannel, StanzaWrapper.wrapper(stanza,205));
            return replies;
        }

        if(!userService.isAdmin(fromEntity)){
            replies.addReply(fromChannel, StanzaWrapper.wrapper(stanza,206));
            return replies;
        }
        List<Channel> toChannels= serverRuntimeContext.getProtocolChannelRegistry().get(toEntity);
        if (!serverRuntimeContext.getProtocolChannelRegistry().isOnline(toEntity)) {
            replies.addReply(fromChannel, StanzaWrapper.wrapperOffline(stanza));
            stanza.setModule("offline");
        } else {
            replies.addReply(fromChannel, StanzaWrapper.wrapperOnline(stanza));
            replies.addReply(toChannels, StanzaWrapper.wrapper(stanza, body));
        }
        serverRuntimeContext.getMessageGatewayPipeline().push(toEntity, stanza);
        if(messagePipeline!=null && ApplicationCostant.ApplicationConfig.getMessageStoreSwitch()==true){
            messagePipeline.messageStore(body);
        }
//      if (getServerRuntimeContext().getMessageStoragePipeline() != null) {
//          getServerRuntimeContext().getMessageStoragePipeline().message(body);
//      }
        return replies;
    }
    public String module() {
        return "chat";
    }
}
